package com.cgwx.tcservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgwx.tcservice.entity.tc.TcFrameConfig;
import com.cgwx.tcservice.mapper.TcFrameConfigMapper;
import com.cgwx.tcservice.service.TcFrameConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * <p>
 * 遥控帧配置表 服务实现类
 * </p>
 *
 * @author GaoWei
 * @since 2023-09-14
 */
@Service
public class TcFrameConfigServiceImpl extends ServiceImpl<TcFrameConfigMapper, TcFrameConfig> implements TcFrameConfigService {

//    更新遥控帧表配置
    @Override
    public void updateTcFrameConfig(MultipartFile file) {
//        获取文件名称
        String uploadFileName = file.getOriginalFilename();
//        获取遥控指令帧名称（去掉.xls/.xlsx）
        String tcFrameName = Objects.requireNonNull(uploadFileName).substring(0,uploadFileName.indexOf('_') + 2);
//        从文件名称中提取关键字，部件代码_指令类型
        String tcFrameIdentifiers = tcFrameName.substring(tcFrameName.indexOf('_') - 2);

//        根据关键字查询是否存在同名遥控帧配置
        QueryWrapper<TcFrameConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tc_frame_identifiers",tcFrameIdentifiers);
        TcFrameConfig queryTcFrameConfig = baseMapper.selectOne(queryWrapper);

//        判断是更新还是插入
        if(queryTcFrameConfig != null){
//            存在配置，进行更新操作
            queryTcFrameConfig.setTcFrameName(tcFrameName);
            queryTcFrameConfig.setTcFrameIdentifiers(tcFrameIdentifiers);
            baseMapper.updateById(queryTcFrameConfig);
        }else{
//            不存在配置，进行插入操作
            TcFrameConfig insertTcFrameConfig = new TcFrameConfig();
            insertTcFrameConfig.setTcFrameName(tcFrameName);
            insertTcFrameConfig.setTcFrameIdentifiers(tcFrameIdentifiers);
            baseMapper.insert(insertTcFrameConfig);
        }
    }
}
